package com.yjhb.android.sbz.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;

import androidx.constraintlayout.widget.ConstraintLayout;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.URLSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yjhb.android.sbz.Conf;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.dialog.SelectedDialog;
import com.yjhb.android.sbz.entity.TaskStep;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.BaseDialog;
import com.jojo.android.zxlib.entity.Result;
import com.jojo.android.zxlib.interfaces.Callback;
import com.jojo.android.zxlib.tool.ZxClipboardTool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.tool.ZxTool;
import com.jojo.android.zxlib.view.preview.PreviewBuilder;
import com.jojo.android.zxlib.view.preview.enitity.IPreviewInfo;
import com.jojo.android.zxlib.view.preview.enitity.ImageViewInfo;
import com.jojo.android.zxlib.view.preview.loader.LongClickListener;

import java.io.File;

/**
 * 创建者 zx
 * 创建时间 2020/11/30 9:08
 */
public class TaskStepItemView extends ConstraintLayout {
    private final String TAG = "TaskStepItemView";
    public final static int BUTTON_UPLOAD_IMG = 1;

    private ImageView imageView;
    private ImageView imageView2;
    private TextView save_tv;
    private LinearLayout upload_layout;
    private TextView tips_tv;
    private TextView title_tv;
    private TextView num_tv;
    private EditText input_et;
    private TextView content_tv;
    private TextView copy_tv;
    private LinearLayout link_layout;
    private TextView link_open_tv;
    private TextView link_copy_tv;
    private LinearLayout scan_layout;
    private TextView scan_scan_tv;
    private TextView scan_save_tv;
    private ViewGroup image1_layout;
    private ViewGroup image2_layout;
    private TextView masking_tv;
    private TextView image_label_tv;

    private String inputContent; //输入的内容

    private TaskStep step;
    private onButtonClickListener onButtonClickListener;
    private boolean isSign = false; //是否已报名，没有报名将展示部分内容
    private boolean isCheck = false;//是否在审核任务界面
    private final String SAVE_PATH = Conf.IMAGE_SAVE_PATH;

    public TaskStepItemView(Context context, TaskStep step) {
        super(context);
        this.step = step;
        init();
    }


    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_task_step_view, this, true);
        imageView = findViewById(R.id.image_iv);
        imageView2 = findViewById(R.id.image2_iv);
        save_tv = findViewById(R.id.save_tv);
        upload_layout = findViewById(R.id.upload_layout);
        tips_tv = findViewById(R.id.tips_tv);
        title_tv = findViewById(R.id.title_tv);
        num_tv = findViewById(R.id.num_tv);
        input_et = findViewById(R.id.et_input);
        content_tv = findViewById(R.id.content_tv);
        copy_tv = findViewById(R.id.copy_tv);
        link_layout = findViewById(R.id.link_layout);
        link_open_tv = findViewById(R.id.link_open_tv);
        link_copy_tv = findViewById(R.id.link_copy_tv);
        scan_layout = findViewById(R.id.scan_layout);
        scan_scan_tv = findViewById(R.id.scan_scan_tv);
        scan_save_tv = findViewById(R.id.scan_save_tv);
        image1_layout = findViewById(R.id.image1_layout);
        image2_layout = findViewById(R.id.image2_layout);
        masking_tv = findViewById(R.id.masking_tv);
        image_label_tv = findViewById(R.id.image_label_tv);


        refresh();
    }


    public void refresh() {
        if (step == null) return;
        title_tv.setText(step.getTitle());
        num_tv.setText("" + (step.getSort() + 1));
        link_layout.setVisibility(GONE);
//        imageView.setVisibility(GONE);
        image1_layout.setVisibility(GONE);
//        imageView2.setVisibility(GONE);
        image2_layout.setVisibility(GONE);
        content_tv.setVisibility(GONE);
        scan_layout.setVisibility(GONE);
        upload_layout.setVisibility(GONE);
        masking_tv.setVisibility(GONE);
        image_label_tv.setVisibility(GONE);
        switch (step.getType()) {
            case 1:
                content_tv.setVisibility(GONE);
                link_layout.setVisibility(VISIBLE);
                SpannableString link = new SpannableString(step.getContent());
                link.setSpan(new URLSpan(step.getContent()), 0, step.getContent().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                content_tv.setText(link);
                content_tv.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isSign) {
                            Tool.startExternalWeb(getContext(), step.getContent());
                        } else {
                            ZxToast.showINFOBottomToast("请先报名哦~");
                        }
                    }
                });
                link_open_tv.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isSign) {
                            Tool.startExternalWeb(getContext(), step.getContent());
//                            Tool.startWebActivity(getContext(), step.getContent(), step.getTitle());
                        } else {
                            ZxToast.showINFOBottomToast("请先报名哦~");
                        }
                    }
                });
                link_copy_tv.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isSign) {
                            ZxClipboardTool.copyText(getContext(), step.getContent());
                            ZxToast.showBottomToastLight("已复制至剪贴板");
                        } else {
                            ZxToast.showINFOBottomToast("请先报名哦~");
                        }
                    }
                });
                break;
            case 2:
                scan_layout.setVisibility(VISIBLE);
                image1_layout.setVisibility(VISIBLE);
                if (masking_tv != null) {
                    masking_tv.setVisibility(isSign ? GONE : VISIBLE);
                }
                Glide.with(getContext()).load(step.getContent()).into(imageView);
                scan_scan_tv.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isSign) {
                            Tool.saveFile(step.getContent(), SAVE_PATH, new Callback<Result<String>>() {
                                @Override
                                public void callback(Result<String> s) {
                                    if (s.getCode() == -1) {
                                        ZxToast.showBottomToastLight("保存失败：" + s.getMessage());
                                    } else {
                                        Tool.parseQRCode(s.getData(), new Callback<String>() {
                                            @Override
                                            public void callback(String s) {
                                                if (TextUtils.isEmpty(s)) {
                                                    ZxToast.showBottomToastLight("识别出错啦~~QAQ");
                                                    return;
                                                }
                                                s = s.trim();
                                                if (s.startsWith("http")) {
                                                    Tool.startExternalWeb(getContext(), s);
                                                } else {
                                                    ZxClipboardTool.copyText(getContext(), s);
                                                    ZxToast.showBottomToastLight("内容已复制至剪切板:" + s);
                                                }
                                            }
                                        });
                                    }
                                }
                            });
                        } else {
                            ZxToast.showINFOBottomToast("请先报名哦~");
                        }
                    }
                });

                scan_save_tv.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isSign) {
                            Tool.saveFile(step.getContent(), SAVE_PATH, new Callback<Result<String>>() {
                                @Override
                                public void callback(Result<String> s) {
                                    if (s.getCode() == -1) {
                                        ZxToast.showBottomToastLight("二维码保存失败o(╥﹏╥)o");
                                        return;
                                    }
                                    Tool.updateGallery(getContext(), new File(s.getData()));
                                    ZxToast.showBottomToastLight("二维码已保存至:" + s.getData());
                                }
                            });
                        } else {
                            ZxToast.showINFOBottomToast("请先报名哦~");
                        }
                    }
                });
                imageView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isSign) {
//                            Tool.startPhotoActivity(getContext(), step.getContent(), v);
                            ImageViewInfo info = new ImageViewInfo(step.getContent());
                            imageView.getGlobalVisibleRect(info.getBounds());
                            PreviewBuilder.from((Activity) getContext())
                                    .setCurrentIndex(0)
                                    .setImg(info)
                                    .setImageLongClick(new LongClickListener() {
                                        @Override
                                        public void onLongClick(Activity activity, IPreviewInfo imageInfo) {
                                            bottomPopup(activity, step.getContent());
                                        }
                                    })
                                    .setDrag(false)
                                    .start();
                        } else {
                            ZxToast.showINFOBottomToast("请先报名哦~");
                        }
                    }
                });
                break;
            case 3:
                image1_layout.setVisibility(VISIBLE);
                image_label_tv.setVisibility(VISIBLE);
                if (isCheck){
                    image_label_tv.setText("验证图");
                    tips_tv.setText("验证图*");
                }else {
                    image_label_tv.setText("说明图");
                    tips_tv.setText("说明图*");
                }
                Glide.with(getContext()).load(step.getContent()).into(imageView);
                tips_tv.setVisibility(VISIBLE);
                imageView.setOnClickListener(v -> {
//                    Tool.startPhotoActivity(getContext(), step.getContent(), v);
//                    List<IPreviewInfo> infos = PreviewUtil.ImagesToPreviewInfo(,imageView);
//                    PreviewBuilder.from((Activity) getContext())
//                            .setCurrentIndex(0)
//                            .setImgs(infos)
//                            .setDrag(false)
//                            .start();
                    ImageViewInfo info = new ImageViewInfo(step.getContent());
                    imageView.getGlobalVisibleRect(info.getBounds());
                    PreviewBuilder.from((Activity) getContext())
                            .setCurrentIndex(0)
                            .setImageLongClick(new LongClickListener() {
                                @Override
                                public void onLongClick(Activity activity, IPreviewInfo imageInfo) {
                                    bottomPopup(activity, step.getContent());
                                }
                            })
                            .setImg(info)
                            .setDrag(false)
                            .start();
                });
                break;
            case 4:

                break;
            case 5:
                copy_tv.setVisibility(VISIBLE);
                content_tv.setVisibility(VISIBLE);
                content_tv.setText(step.getContent());
                copy_tv.setOnClickListener(v -> {
                    if (isSign) {
                        ZxClipboardTool.copyText(getContext(), step.getContent());
                        ZxToast.showBottomToastLight("已复制至剪贴板");
                    } else {
                        ZxToast.showINFOBottomToast("请先报名哦~");
                    }
                });
                break;
            case 6:
                image1_layout.setVisibility(VISIBLE);
                image_label_tv.setVisibility(VISIBLE);
                image_label_tv.setText("验证图");
                Glide.with(getContext()).load(step.getContent()).into(imageView);
                upload_layout.setVisibility(VISIBLE);
                tips_tv.setText("示例图*");
                tips_tv.setVisibility(VISIBLE);
                upload_layout.setOnClickListener(v -> {
                    if (isSign) {
                        if (onButtonClickListener != null) {
                            onButtonClickListener.onButtonClick(BUTTON_UPLOAD_IMG);
                        }
                    } else {
                        ZxToast.showINFOBottomToast("请先报名哦~");
                    }
                });
                imageView.setOnClickListener(v -> {
                    ImageViewInfo info = new ImageViewInfo(step.getContent());
                    imageView.getGlobalVisibleRect(info.getBounds());
                    PreviewBuilder.from((Activity) getContext())
                            .setCurrentIndex(0)
                            .setImageLongClick(new LongClickListener() {
                                @Override
                                public void onLongClick(Activity activity, IPreviewInfo imageInfo) {
                                    bottomPopup(activity, step.getContent());
                                }
                            })
                            .setImg(info)
                            .setDrag(false)
                            .start();
                });
                imageView2.setOnClickListener(v -> {
                    if (onButtonClickListener != null) {
                        onButtonClickListener.onButtonClick(BUTTON_UPLOAD_IMG);
                    }
                });
                if (!TextUtils.isEmpty(inputContent)) {
                    image2_layout.setVisibility(VISIBLE);
                    upload_layout.setVisibility(GONE);
                    Glide.with(getContext()).load(inputContent).into(imageView2);
                } else {
                    image2_layout.setVisibility(GONE);
                    upload_layout.setVisibility(VISIBLE);
                }
                break;
            case 7:
                input_et.clearFocus();
                input_et.setFocusable(isSign);
                input_et.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isSign) {
                            input_et.setFocusableInTouchMode(true);
                            input_et.requestFocus();
                            ZxTool.openKeybord(input_et, getContext());
                        } else {

                            ZxToast.showINFOBottomToast("请先报名哦~");
                        }
                    }
                });
                input_et.setVisibility(VISIBLE);
                input_et.setText(inputContent);
                break;
        }

    }

    private void bottomPopup(Activity context, String url) {

        SelectedDialog tipsDialog = new SelectedDialog(context);
        tipsDialog.setOnClickListener(new BaseDialog.OnClickListener() {
            @Override
            public void onClick(Dialog dialog, int button) {
                if (button == 1) {
                    Tool.saveFile(url, SAVE_PATH, new Callback<Result<String>>() {
                        @Override
                        public void callback(Result<String> stringResult) {
                            if (stringResult.getCode() == -1) {
                                ZxToast.showFAILBottomToast("识别失败：图片文件获取失败！");
                            } else {
                                Tool.parseQRCode(stringResult.getData(), new Callback<String>() {
                                    @Override
                                    public void callback(String s) {
                                        if (!TextUtils.isEmpty(s)) {
                                            s = s.trim();
                                            if (s.startsWith("http")) {
                                                Tool.startExternalWeb(getContext(), s);
                                            } else {
                                                ZxClipboardTool.copyText(getContext(), s);
                                                ZxToast.showSuccessBottomToast("识别内容已复制至剪切板");
                                            }
                                        } else {
                                            ZxToast.showFAILBottomToast("识别失败！");
                                        }
                                    }
                                });
                            }
                        }
                    });
                } else if (button == 2) {
                    Tool.saveFile(url, SAVE_PATH, new Callback<Result<String>>() {
                        @Override
                        public void callback(Result<String> stringResult) {
                            if (stringResult.getCode() == -1) {
                                ZxToast.showINFOBottomToast("保存失败：" + stringResult.getData());
                            } else {
                                Tool.updateGallery(getContext(), new File(stringResult.getData()));
                                ZxToast.showSuccessBottomToast("保存成功：" + stringResult.getData());
                            }
                        }
                    });
                }
                dialog.dismiss();
            }
        });
        tipsDialog.show();
    }


    public TaskStep getStep() {
        return step;
    }

    public String getInputText() {
        if (step.getType() == 6) {
            return inputContent;
        }
        if (step.getType() == 7) {
            return Tool.getText(input_et, "");
        }
        return null;
    }

    public void setSign(boolean b) {
        this.isSign = b;
        refresh();
    }

    public void setCheck(boolean check) {
        isCheck = check;
    }

    public void setInputContent(String content) {
        this.inputContent = content;
    }


    public void setOnButtonClickListener(TaskStepItemView.onButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }

    public interface onButtonClickListener {
        void onButtonClick(int button);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            if (view instanceof ViewGroup) {
                for (int j = 0; j < ((ViewGroup) view).getChildCount(); j++) {
                    ((ViewGroup) view).getChildAt(j).setEnabled(enabled);
                }
            }
        }
    }

}
